<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <!-- 避免IE使用兼容模式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <!-- TopJUI框架样式 -->
    <link type="text/css" href="/topjui/css/topjui.core.min.css" rel="stylesheet">
    <link type="text/css" href="/topjui/themes/default/topjui.bluelight.css" rel="stylesheet" id="dynamicTheme"/>
    <!-- FontAwesome字体图标 -->
    <link type="text/css" href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>
    <!-- layui框架样式 -->
    <link type="text/css" href="/static/plugins/layui/css/layui.css" rel="stylesheet"/>
    <link type="text/css" href="/topjui/css/myui.css" rel="stylesheet"/>
    <script type="text/javascript" src="/static/public/js/topjui.config.js"></script>
    <!-- layui框架js -->
    <script type="text/javascript" src="/static/plugins/layui/layui.js" charset="utf-8"></script>
    <script src="/easyui/jquery.min.js"></script>
    <script src="/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="/topjui/js/sysutils.js"></script>
</head>

<body>

<table id="rowgrid" class="topjui-datagrid"></table>

<script type="text/javascript" charset="utf-8" th:inline="none">
    var listgrid;
    $(function(){
        listgrid = $('#rowgrid').datagrid({
            url:"/jdgoods/list",
            method:'get',
            fit : true,
            fitColumns : true,
            border : false,
            pagination : true,
            idField : 'id',
            pageSize : 20,
            pageList : [ 20, 50, 100 ],
            sortName : 'id',
            sortOrder : 'desc',
            checkOnSelect : true,
            selectOnCheck : true,
            queryParams:{
                "status":0
            },
            nowrap : false,
            columns:[[
                {
                    field:'id',
                    title:'编号',
                    width:50,
                    checkbox : true
                }, {
                    field:'skuid',
                    title:'产品编码',
                    sortable:true,
                    width:40
                }, {
                    field:'goodsname',
                    title:'产品名称',
                    sortable:true,
                    width:90
                }, {
                    field:'price',
                    title:'市场价',
                    width:30
                }, {
                    field:'sellprice',
                    title:'售价',
                    width:30
                }, {
                    field:'typeid',
                    title:'类ID',
                    width:30
                }, {
                    field:'typename',
                    title:'所属类',
                    width:30
                }, {
                    field:'subtypeid',
                    title:'子类ID',
                    width:30
                }, {
                    field:'subtypename',
                    title:'所属子类',
                    width:30
                }, {
                    field:'goodsurl',
                    title:'产品地址',
                    width:100
                }, {
                    field:'brandid',
                    title:'品牌ID',
                    width:30
                }, {
                    field:'brand',
                    title:'品牌',
                    width:30
                }, {
                    field:'goodsno',
                    title:'商品货号',
                    width:30
                },  {
                    field:'down',
                    title:'是否下载',
                    width:30,
                    align:'center',
                    formatter : function(value, row, index) {
                        if(value == '1'){
                            return '<span style="color:green">已完成</span>';
                        }else{
                            return '<span style="color:red">未完成</span>';
                        }
                    }
                },  {
                    field:'success',
                    title:'是否上传',
                    width:30,
                    align:'center',
                    formatter : function(value, row, index) {
                        if(value == '1'){
                            return '<span style="color:green">已完成</span>';
                        }else{
                            return '<span style="color:red">未完成</span>';
                        }
                    }
                },  {
                    field:'status',
                    title:'是否完成',
                    width:30,
                    align:'center',
                    formatter : function(value, row, index) {
                        if(value == '1'){
                            return '<span style="color:green">已完成</span>';
                        }else{
                            return '<span style="color:red">未完成</span>';
                        }
                    }
                }, {
                    field : 'action',
                    title : '操作',
                    width : 50,
                    align:'center',
                    formatter : function(value, row, index) {
                        return formatString(
                                '<img onclick="list_downFun1(\'{0}\');" title="下载数据" style="cursor:pointer" width="18" height="18" src="{1}"/>&nbsp;&nbsp;&nbsp;' +
                                '<img onclick="list_downFun(\'{2}\');" title="下载数据" style="cursor:pointer" src="{3}"/>&nbsp;&nbsp;&nbsp;' +
                                '<img onclick="list_editFun(\'{4}\');" title="添加商品" style="cursor:pointer" src="{5}"/>',
                                row.id, '/static/admin/images/down1.png',
                                row.id, '/static/admin/images/down.png',
                                row.id, '/static/admin/images/up.png');
                    }
                }
            ]],
            toolbar:[{
                text:'已完成',
                iconCls : 'fa fa-search',
                btnCls: 'topjui-btn-normal',
                handler : function() {
                    var queryParams = {"status":1}
                    listgrid.datagrid('options').queryParams=queryParams;
                    listgrid.datagrid('reload');
                }
            },'-',{
                text:'未完成',
                iconCls : 'fa fa-search',
                btnCls: 'topjui-btn-normal',
                handler : function() {
                    var queryParams = {"status":0}
                    listgrid.datagrid('options').queryParams=queryParams;
                    listgrid.datagrid('reload');
                }
            },'-',{
                text:'查询',
                iconCls : 'fa fa-search',
                btnCls: 'topjui-btn-normal',
                handler : function() {
                    list_search();
                }
            },'-',{
                text:'更新数据',
                iconCls : 'fa fa-book',
                btnCls: 'topjui-btn-normal',
                handler : function() {
                    list_removeFun();
                }
            },'-',{
                text:'忽略',
                iconCls:'fa fa-trash',
                btnCls: 'topjui-btn-normal',
                handler:function(){
                    list_removeFun1();
                }
            },'-', {
                text:'刷新',
                iconCls : 'fa fa-refresh',
                btnCls: 'topjui-btn-green',
                handler : function() {
                    listgrid.datagrid('reload');
                }
            }],
            onDblClickRow :function(rowIndex,rowData){
                list_lookFun(rowData.id);
            }
        });
    });

    function list_removeFun1() {
        var rows = listgrid.datagrid('getChecked');
        var ids = [];
        if (rows.length > 0) {
            $.messager.confirm('确认', '您是否要忽略当前选中的项目？', function(r) {
                if (r) {
                    for ( var i = 0; i < rows.length; i++) {
                        ids.push(rows[i].id);
                    }
                    $.ajax({
                        url : '/jdgoods/ignoreupdate',
                        data : {
                            ids : ids.join(',')
                        },
                        dataType : 'json',
                        success : function(r) {
                            if (r.statusCode=='200') {
                                listgrid.datagrid('reload');
                                listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                            }
                            $.messager.show({
                                title : r.title,
                                msg : r.message
                            });
                        }
                    });
                }
            });
        } else {
            $.messager.show({
                title : '提示',
                msg : '请勾选要删除的记录！'
            });
        }
    }

    function list_search(){
        $('<div/>').dialog({
            href : '/jdgoods/search',
            width : 370,
            height : 290,
            modal : true,
            title : '查询',
            buttons : [{
                text : '查询',
                iconCls : 'fa fa-search',
                btnCls: 'topjui-btn-normal',
                handler : function() {
                    var d = $(this).closest('.window-body');
                    var param = form2Json("search_form");
                    $('#search_form').form('submit', {
                        url : '/jdgoods/list',
                        onSubmit:function(){
                            return true;
                        },
                        success : function(result) {
                            try {
                                var r = $.parseJSON(result);
                                listgrid.datagrid({
                                    url:'/jdgoods/list',
                                    pagination : true,
                                    idField : 'id',
                                    pageSize : 20,
                                    pageList : [ 20, 50, 100 ],
                                    sortName : 'id',
                                    sortOrder : 'desc',
                                    queryParams: param,　　//关键之处
                                    data:r
                                });
                                d.dialog('destroy');

                            } catch (e) {
                                $.messager.alert('提示', result);
                            }
                        }
                    });
                }
            }],
            onClose : function() {
                $(this).dialog('destroy');
            }
        });
    }

    function list_downFun(id) {
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        $.messager.confirm('确认', '您是否要下载当前选中的商品数据？', function (r) {
            if (r) {
                EasyUILoad(); //显示效果
                $.ajax({
                    url: '/jdgoods/download',
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    success: function (r) {
                        dispalyEasyUILoad(); //隐藏效果
                        if (r.statusCode=='200') {
                            //TODO: 添加分页参数

                            //listgrid.datagrid('options').queryParams=queryParams;
                            listgrid.datagrid('reload');
                            listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                        }
                        $.messager.show({
                            title: '提示',
                            msg: r.message
                        });
                    },
                    error:function (xhr,state,errorThrown) {
                        $.messager.show({
                            title: '提示',
                            msg: xhr
                        });
                    }
                });
            }
        });
    }

    function list_downFun1(id) {
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        $.messager.confirm('确认', '您是否要下载当前选中的商品数据？', function (r) {
            if (r) {
                EasyUILoad(); //显示效果
                $.ajax({
                    url: '/jdgoods/download1',
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    success: function (r) {
                        dispalyEasyUILoad(); //隐藏效果
                        if (r.statusCode=='200') {
                            //TODO: 添加分页参数

                            //listgrid.datagrid('options').queryParams=queryParams;
                            listgrid.datagrid('reload');
                            listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                        }
                        $.messager.show({
                            title: '提示',
                            msg: r.message
                        });
                    },
                    error:function (xhr,state,errorThrown) {
                        $.messager.show({
                            title: '提示',
                            msg: xhr
                        });
                    }
                });
            }
        });
    }

    function list_editFun(id) {
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        $('<div/>').dialog({
            href : "/jdgoods/form",
            width : 900,
            height : 360,
            modal : true,
            title : '编辑信息',
            buttons : [ {
                text : '保存',
                iconCls : 'fa fa-save',
                btnCls: 'topjui-btn-normal',
                handler : function() {
                    var d = $(this).closest('.window-body');
                    $('#customerform').form('submit', {
                        url : "/goods/saveGoods",
                        success : function(result) {
                            try {
                                var r = $.parseJSON(result);
                                if (r.statusCode=='200') {
                                    listgrid.datagrid('reload');
                                    d.dialog('destroy');
                                }
                                $.messager.show({
                                    title : r.title,
                                    msg : r.message
                                });
                            } catch (e) {
                                $.messager.alert('提示', result);
                            }
                        }
                    });
                }
            } ],
            onClose : function() {
                $(this).dialog('destroy');
            },
            onLoad : function() {
                var index = listgrid.datagrid('getRowIndex', id);
                var rows = listgrid.datagrid('getRows');
                var o = rows[index];
                o.brandId = o.brandid;
                o.goodssum = 500;
                $('#customerform').form('load', o);
            }
        });
    }

    function list_removeFun() {
        var rows = listgrid.datagrid('getChecked');
        var ids = [];
        if (rows.length > 0) {
            for ( var i = 0; i < rows.length; i++) {
                ids.push(rows[i].id);
            }
            console.log(ids)
            $('<div/>').dialog({
                href : '/jdgoods/update',
                width : 370,
                height : 260,
                modal : true,
                title : '更新数据',
                buttons : [ {
                    text : '保存',
                    iconCls : 'fa fa-save',
                    btnCls: 'topjui-btn-normal',
                    handler : function() {
                        var d = $(this).closest('.window-body');
                        $('#search_form').form('submit', {
                            url : "/jdgoods/allupdate",
                            success : function(result) {
                                try {
                                    var r = $.parseJSON(result);
                                    if (r.statusCode=='200') {
                                        listgrid.datagrid('reload');
                                        d.dialog('destroy');
                                    }
                                    $.messager.show({
                                        title : r.title,
                                        msg : r.message
                                    });
                                } catch (e) {
                                    $.messager.alert('提示', result);
                                }
                            }
                        });
                    }
                } ],
                onClose : function() {
                    $(this).dialog('destroy');
                },
                onLoad : function() {
                    $('#ids').val(ids)
                }
            });
        } else {
            $.messager.show({
                title : '提示',
                msg : '请勾选要删除的记录！'
            });
        }
    }
    
    function list_createFun(id,goodsno) {
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        if(goodsno==undefined || goodsno=='' || goodsno=='null'){
            $.messager.alert('提示', '您的商品信息不全，请修改后在试图上传！');
        }else{

        }
    }

    function EasyUILoad() {
        $("<div class=\"datagrid-mask\"></div>").css({ display: "block", width: "100%", height: "auto !important" }).appendTo("body");
        $("<div class=\"datagrid-mask-msg\"></div>").html("<img  class ='img1' /> 正在下载商品数据，请稍候...").appendTo("body").css({ display: "block", left: ($(document.body).outerWidth(true) - 190) / 2, top: ($(window).height() - 45) / 2 });
    }

    function dispalyEasyUILoad() {
        $(".datagrid-mask").remove();
        $(".datagrid-mask-msg").remove();
    }

</script>

</body>
</html>